package com.jerry.android.TabBar.radiobutton;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.blankj.utilcode.util.SizeUtils;
import com.jerry.android.R;
import com.jerry.android.TabBar.fragment.AddressFragment;
import com.jerry.android.TabBar.fragment.FrdFragment;
import com.jerry.android.TabBar.fragment.SettingFragment;
import com.jerry.android.TabBar.fragment.WeixinFragment;

public class TabRadioButtonActivity extends FragmentActivity {

    private RadioGroup mTabRadioGroup;
    protected FragmentManager mFragmentManager;
    public Fragment mCurrentFragment;
    private Fragment mFragment01;
    private Fragment mFragment02;
    private Fragment mFragment03;
    private Fragment mFragment04;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_tab_radiobutton);

        initTabView();
        initFragment();
    }

    @Override
    public void onResume() {
        super.onResume();

        System.out.println("TabRadioButton Activity ==== onResume()");
    }

    private void initTabView() {
        mTabRadioGroup = (RadioGroup) findViewById(R.id.rg);

        // 动态设置大小
        RadioButton homeRadioButton = (RadioButton) findViewById(R.id.rb_home);
        RadioButton msgRadioButton = (RadioButton) findViewById(R.id.rb_mes);
        RadioButton findRadioButton = (RadioButton) findViewById(R.id.rb_find);
        RadioButton myRadioButton = (RadioButton) findViewById(R.id.rb_my);
        initDrawableSize(homeRadioButton);
        initDrawableSize(msgRadioButton);
        initDrawableSize(findRadioButton);
        initDrawableSize(myRadioButton);
    }

    private void initDrawableSize(RadioButton radioButton){
        Drawable[] rb_drawables = radioButton.getCompoundDrawables();
        int pointX = 0;
        int pointY = 0;
        int width = SizeUtils.dp2px(33);
        int height = SizeUtils.dp2px(33);
        rb_drawables[1].setBounds(pointX, pointY, width, height);
        radioButton.setCompoundDrawables(null, rb_drawables[1], null, null);   //左上右下
    }

    private void initFragment() {
        mFragmentManager = getSupportFragmentManager();
        mTabRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentTransaction ft = mFragmentManager.beginTransaction();
                if (mCurrentFragment != null) {
                    ft.hide(mCurrentFragment);
                }
                switch (checkedId) {
                    case R.id.rb_home:
                        showFragment01(ft);
                        break;

                    case R.id.rb_find:
                        showFragment02(ft);
                        break;

                    case R.id.rb_mes:
                        showFragment03(ft);
                        break;

                    case R.id.rb_my:
                        showFragment04(ft);
                        break;
                }
                ft.commitAllowingStateLoss();
            }
        });
        mTabRadioGroup.check(R.id.rb_home);
    }

    private void showFragment01(FragmentTransaction ft) {
        if (mFragment01 == null) {
            mFragment01 = new WeixinFragment();
            ft.add(R.id.container, mFragment01);
        }
        mCurrentFragment = mFragment01;
        ft.show(mFragment01);
    }

    private void showFragment02(FragmentTransaction ft) {
        if (mFragment02 == null) {
            mFragment02 = new FrdFragment();
            ft.add(R.id.container, mFragment02);
        }
        mCurrentFragment = mFragment02;
        ft.show(mFragment02);
    }

    private void showFragment03(FragmentTransaction ft) {
        if (mFragment03 == null) {
            mFragment03 = new AddressFragment();
            ft.add(R.id.container, mFragment03);
        }
        mCurrentFragment = mFragment03;
        ft.show(mFragment03);
    }

    private void showFragment04(FragmentTransaction ft) {
        if (mFragment04 == null) {
            mFragment04 = new SettingFragment();
            ft.add(R.id.container, mFragment04);
        }
        mCurrentFragment = mFragment04;
        ft.show(mFragment04);
    }


}
